<?php
namespace data\service;

use app\api\utils\ArrayUtil;
use data\model\CategoryModel;

class CategoryService extends BaseService {

    private $categoryModel = null;

    public function __construct() {
        parent::__construct();
        $this->categoryModel = new CategoryModel();
    }
    public function fieldInc($id,$field,$step = 1){
        $res = $this->categoryModel->where('id='.$id)->setInc($field,$step);
        return $res;
    }

    public function getCategorys( $page , $pageSize , $where , $order = "",$fields = "*"){
        $categoryLists = $this->categoryModel->pageQuery($page , $pageSize , $where , $order ,$fields );
        $categories = [];
        if(!empty($categoryLists['data'])){
            foreach($categoryLists['data'] as $k => $v){
                $categories[$k] = [
                    'id' => $v['id'],
                    'name' => $v['name'],
                    'thumb' => $v['img'].IMG_STYLE,
                    'favs' => $v['favs'],
                    'hits' => $v['hits'],
                ];
            }
            return $categories;
        }
        return [];
    }

    public function categoryDetail( $catId = 0 ,$fields = "*" ){
        $catDetail = $this->categoryModel->getInfo( ['id' => $catId] ,$fields) ;
        $catDetail['img'] = $catDetail['img'].IMG_STYLE;
        if(!empty($catDetail)){
            return $catDetail;
        }
        return [];
    }



}
